How to do it?:
Open the Rmarkdown file of this assignment (link) in Rstudio.
Right under each question, insert a code chunk
(you can use the hotkey Ctrl + Alt + I to add a code chunk)
and code the solution for the question.
Knit the rmarkdown file (hotkey:
Ctrl + Alt + K) to export an html.
Publish the html file to your Githiub Page.
Submission: Submit the link on Github of the assignment to Canvas
gganimate and gifski
then restart Rstudio. Using the Adult Census Income data,
make an animation using geom_point and
transition_states.install.packages('gganimate')
install.packages('gifski')
df <- read.csv('adult_census_missing.csv')
names(df)
## [1] "age" "workclass" "fnlwgt" "education"
## [5] "education.num" "marital.status" "occupation" "relationship"
## [9] "race" "sex" "capital.gain" "capital.loss"
## [13] "hours.per.week" "native.country" "income"
library(gganimate)
## Warning: package 'gganimate' was built under R version 4.2.2
library(gifski)
## Warning: package 'gifski' was built under R version 4.2.2
library(ggplot2)
library(dplyr)
library(tidyverse)
df <- df %>%
na_if("Not known") %>%
na_if("?") %>%
na_if("Unknown")
df <- drop_na(df)
g <- ggplot(df, aes(x = hours.per.week, y = capital.gain))+
geom_point() +
transition_states(sex)+
labs(title ='{closest_state}')
animate(g)
Adult Census Income data, make an animation
using geom_bar and transition_states.g <- ggplot(df, aes(x = workclass))+
geom_bar() +
transition_states(sex)+
labs(title ='{closest_state}')
animate(g)
library(lubridate)
library(knitr)
df3 <- read.csv('WHO-COVID-19-global-data.csv')
df3$week <- week(df3$Date_reported)
df3 <- df3 %>%
group_by(week, Country) %>%
summarise(mean = mean(New_cases)) %>%
group_by(week) %>% mutate(rank=rank(-mean)) %>%
filter(rank <= 10) %>%
ggplot(aes(x=rank, y=mean, group=Country, fill=Country, label=Country)) +
geom_col()+
geom_text(aes(y = mean, label = Country), hjust = 1.4)+
coord_flip(clip = "off", expand = FALSE) +scale_x_reverse()+
labs(title = 'Week {closest_state}', x='', y='Number of Positive Cases', fill='Country')+
theme(plot.title = element_text(hjust = 1, size = 22), axis.ticks.y = element_blank(), axis.text.y = element_blank()) +
transition_states(week)+
ease_aes("cubic-in-out")
animate(df3, nframes = 1000)
df4 <- read.csv('all-states-history.csv')
names(df4)
## [1] "date" "state"
## [3] "death" "deathConfirmed"
## [5] "deathIncrease" "deathProbable"
## [7] "hospitalized" "hospitalizedCumulative"
## [9] "hospitalizedCurrently" "hospitalizedIncrease"
## [11] "inIcuCumulative" "inIcuCurrently"
## [13] "negative" "negativeIncrease"
## [15] "negativeTestsAntibody" "negativeTestsPeopleAntibody"
## [17] "negativeTestsViral" "onVentilatorCumulative"
## [19] "onVentilatorCurrently" "positive"
## [21] "positiveCasesViral" "positiveIncrease"
## [23] "positiveScore" "positiveTestsAntibody"
## [25] "positiveTestsAntigen" "positiveTestsPeopleAntibody"
## [27] "positiveTestsPeopleAntigen" "positiveTestsViral"
## [29] "recovered" "totalTestEncountersViral"
## [31] "totalTestEncountersViralIncrease" "totalTestResults"
## [33] "totalTestResultsIncrease" "totalTestsAntibody"
## [35] "totalTestsAntigen" "totalTestsPeopleAntibody"
## [37] "totalTestsPeopleAntigen" "totalTestsPeopleViral"
## [39] "totalTestsPeopleViralIncrease" "totalTestsViral"
## [41] "totalTestsViralIncrease"
library(lubridate)
library(knitr)
df4$week <- week(df4$date)
df4 <- df4 %>%
group_by(week, state) %>%
summarise(mean = mean(positive)) %>%
group_by(week) %>% mutate(rank=rank(-mean)) %>%
filter(rank <= 10) %>%
ggplot(aes(x=rank, y=mean, group=state, fill=state, label=state)) +
geom_col()+
geom_text(aes(y = mean, label = state), hjust = 1.4)+
coord_flip(clip = "off", expand = FALSE) +scale_x_reverse()+
labs(title = 'Week {closest_state}', x='', y='Number of Positive Cases', fill='State')+
theme(plot.title = element_text(hjust = 1, size = 22), axis.ticks.y = element_blank(), axis.text.y = element_blank()) +
transition_states(week)+
ease_aes("cubic-in-out")
animate(df3, nframes = 1000)